<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <script>
      /*  给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。
  示例 1：
  输入：matrix = [
     [1,2,3],
     [4,5,6],
     [7,8,9]]

 输出：[1,2,3,6,9,8,7,4,5] 示例 2：


  输入：matrix = [
     [1, 2, 3, 4],
     [5, 6, 7, 8],
     [9,10,11,12]


     ]
      输出：[1,2,3,4,8,12,11,10,9,5,6,7]
  
   来源：力扣（LeetCode）
  链接：https://leetcode-cn.com/problems/spiral-matrix
     
         著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。*/
      const matrix = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
      ];
      for (let i = 0; i < 6; ++i) {
        console.log(i);
      }

      for (let i = 0; i < 6; i++) {
        console.log(i);
      }

      var spiralOrder = function (matrix) {
        let left = 0,
          right = matrix[0].length - 1;
        let top = 0,
          down = matrix.length - 1;
        let arr = [];

        while (true) {
          for (let i = left; i <= right; i++) {
            arr.push(matrix[top][i]);
          }
          top++;
          if (top > down) break;
          for (let i = top; i <= down; i++) {
            arr.push(matrix[i][right]);
          }
          right--;
          if (left > right) break;
          for (let i = right; i >= left; --i) {
            arr.push(matrix[down][i]);
          }
          down--;
          if (top > down) break;
          for (let i = down; i >= top; --i) {
            arr.push(matrix[i][left]);
          }
          left++;

          if (left > right) break;
        }
        return arr;
      };

      console.log(spiralOrder(matrix));
    </script>
  </body>
</html>
